<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>员工管理</title>
    <#include "../common/link.ftl">
    <script>
        $(function () {
            $(".btn_delete").click(function () {
                var id = $(this).data("id");
                $.messager.confirm("提示","确定目标？",function () {
                    $.get("/employee/delete.do",{id:id},function(data) {
                        if (data.success) {
                            /*重新加载页面*/
                            $.messager.popup("温馨提示：操作成功了呢~ 2s 后自动关闭");
                            window.setTimeout(function () {
                                window.location.href="/employee/list.do";
                            }, 2000);
                        } else {
                            alert("操作失败")
                            $.messager.popup(data.msg);
                        }
                    })
                })
            })
            $(".btn-import").click(function () {
                $("#myModal").modal('show');
            })
            $(".btn-submitsave").click(function () {
                $('#editForm').ajaxSubmit(function(data) {
                    console.log(data.success)
                    if (data.success) {
                        /*重新加载页面*/
                        $.messager.popup("温馨提示：操作成功了呢~ 2s 后自动关闭");
                        window.setTimeout(function () {
                            window.location.href="/employee/list.do";
                        }, 2000);
                    } else {
                        $.messager.popup("不对劲！！");
                    }
                });
            })
        })
        // 完成效果，当三条记录都挨个被选中时，那么最上面的全选也是被选中状态，
        //如果去向记录中的一个，那么最上面也要去掉对号
        $(function () {
            //全选/全不选
            $("#allCb").click(function(){
                //获取当前复选框的状态checked
                //设置到table中的所有复选框
                $('.cb').prop('checked',$(this).prop('checked'))
            })
            $('.cb').click(function(){
                //获取已选中的复选框数量，判断下一额是否等于当前的TABLE中的
                //复选框的数量
                $('#allCb').prop('checked',$('.cb:checked').length == $('.cb').length);
            })

            //获取被选中的ID
            $('.btn_abtchDelete').click(function(){
                //获取勾选的数据
                //如果没有数据，需要先提示一下。请选择要删除的数据
                if($('.cb:checked').length==0){
                    $.messager.alert("Warnning","请选择要删除的记录");
                    return;
                }

                //获取选中的记录ID。保存到数组中
                var cbs = $('.cb:checked');
                var ids = [];
                $.each(cbs,function (index,ele) {
                    ids.push($(ele).data('id'));
                })

                /*var ids = $.map(cbs,function (ele) {
                    return $(ele).data('id');
                })*/
                //JQ。传递过去的数据。JQ会再封装一层括号{}，在原有[]数组基础上再封装一份{}
                //{[4,5]}
                jQuery.ajaxSettings.traditional = true;
                console.log(ids);
                $.messager.confirm("警告","请确认是否删除选中的记录",function (data) {
                    $.get("/employee/abtchDelete.do",{ids:[ids]},function(data) {
                        if (data.success) {
                            /*重新加载页面*/
                            $.messager.popup("温馨提示：操作成功了呢~ 2s 后自动关闭");
                            window.setTimeout(function () {
                                window.location.href="/employee/list.do";
                            }, 2000);
                        } else {
                            $.messager.popup(data.msg);
                        }
                    });
                })
            })
        })
    </script>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
    <#include "../common/navbar.ftl">
    <!--菜单回显-->
    <#assign currentMenu="employee"/>
    <#include "../common/menu.ftl">
    <div class="content-wrapper">
        <section class="content-header">
            <h1>员工管理</h1>
        </section>
        <section class="content">
            <div class="box">
                <!--高级查询-->
                <div style="margin: 10px;">
                    <form class="form-inline" id="searchForm" action="/employee/list.do" method="post">
                        <input type="hidden" name="currentPage" id="currentPage" value="1">
                        <div class="form-group">
                            <label for="keyword">关键字:</label>
                            <input type="text" class="form-control" id="keyword" name="keyword" value="${qo.keyword!}" placeholder="请输入姓名/邮箱">
                        </div>
                        <div class="form-group">
                            <label for="dept"> 部门:</label>
                            <select class="form-control" id="dept" name="deptId">
                                <option value="-1">全部</option>
                                <#list depts as d>
                                    <option value="${d.id}">${d.name!}</option>
                                </#list>
                            </select>
                            <script>
                                /*$("#dept option[value='${qo.deptId!}']").prop("selected", true);*/
                                $("#dept").val(${qo.deptId!});
                            </script>
                        </div>
                        <button id="btn_query" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> 查询</button>
                        <a href="/employee/input.do" class="btn btn-success btn_redirect">
                            <span class="glyphicon glyphicon-plus"></span> 添加
                        </a>

                        <a href="#" class="btn btn-danger btn_abtchDelete">
                            <span class="glyphicon glyphicon-plus"></span> 批量删除
                        </a>

                        <#--day0928 导入导出-->
                        <a href="#" class="btn btn-warning">
                            <span class="glyphicon glyphicon-import btn-import">导入</span>
                        </a>
                        <a href="/employee/exprotxls.do" class="btn btn-warning">
                            <span class="glyphicon glyphicon-export">导出</span>
                        </a>
                    </form>
                </div>
                <table class="table table-hover table-bordered">
                    <thead>
                    <tr>
                        <th><input type="checkbox" id="allCb"></th>
                        <th>编号</th>
                        <th>名称</th>
                        <th>email</th>
                        <th>年龄</th>
                        <th>部门</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <#list result.list as entity>
                        <tr>
                            <td><input type="checkbox" class="cb" data-id="${entity.id!}"></td>
                            <td>${entity_index+1}</td>
                            <td>${entity.name!}</td>
                            <td>${entity.email!}</td>
                            <td>${entity.age!}</td>
                            <td>${(entity.dept.name)!}</td>
                            <td>
                                <a href="/employee/input.do?id=${entity.id!}" class="btn btn-info btn-xs btn_redirect">
                                    <span class="glyphicon glyphicon-pencil"></span> 编辑
                                </a>
                                <a href="#" class="btn btn-danger btn-xs btn_delete" data-id="${(entity.id)!}">
                                    <span class="glyphicon glyphicon-trash"></span> 删除
                                </a>
                            </td>
                        </tr>
                    </#list>
                </table>
                <!--分页-->
                <#include "../common/page.ftl">
            </div>
        </section>
    </div>
    <#include "../common/footer.ftl">
</div>
<#--模态框-->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true"></span></button>
                <h4 class="modal-title" id="myModalLabel">员工导入</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/employee/importxls.do" method="post" id="editForm">
                    <input type="hidden" name="id">
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="name" class="col-sm-3 control-label">导入文件：</label>
                        <div class="col-sm-6">
                            <input type="file" name="file">
                        </div>
                    </div>
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="sn" class="col-sm-3 control-label">编码：</label>
                        <div class="col-sm-6">
                            <a href="/xlstemplates/employee_import.xls" class="btn btn-success">
                                <span class="glyphicon glyphicon-export"></span>下载模板
                            </a>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary btn-submitsave">保存</button>
            </div>
        </div>
    </div>
</div>

</body>
</html>
